package com.zeng.sys.filemd.restfulmodule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.MapBuilder;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.sys.filemd.servicemodule.vo.FileTypeVo;
import com.zeng.sys.filemd.servicemodule.service.FileTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/fileType")
@RestController
public class FileTypeController {

    @Autowired
    FileTypeService fileTypeService;

    @RequestMapping("/list")
    public Object list(Page page, FileTypeVo fileTypeVo) {
        List<FileTypeVo> page1 = this.fileTypeService.findPage(page, fileTypeVo);

        ArrayList<Map<String,Object>> objects = new ArrayList<>();
        page1.forEach(e->{
            Map<String, Object> build = MapBuilder.newBuilder()
                    .put("code", e.getCode())
                    .put("updateDate", e.getUpdateDate())
                    .put("insertDate", e.getInsertDate())
                    .put("id", e.getId())
                    .put("name", e.getName())
                    .put("flag", e.getFlag())
                    .put("typeLimit", e.getTypeLimit())
                    .put("status", e.getStatus())
                    .put("sizeLimit", e.getSizeLimit())
                    .build();
            objects.add(build);

        });
        return ResponseUtil.success("操作成功")
                .addData("fileType", objects)
                .addData("page", page);

    }

    @RequestMapping("/detail")
        public Object detail(String code) {

        return ResponseUtil.success("操作成功").addData("fileType", fileTypeService.getOneByCode(code));

    }

    @RequestMapping("/add")
    public Object add(FileTypeVo fileTypeVo) {

        return ResponseUtil.success("操作成功").addData("fileType", fileTypeService.addOne(fileTypeVo));

    }

    @RequestMapping("/update")
    public Object update(FileTypeVo fileTypeVo) {
        Integer update = this.fileTypeService.update(fileTypeVo);

        return ResponseUtil.success("操作成功")
                .addData("updateCount", update);
    }

    @RequestMapping("/delete")
    public Object delete(String code) {
        Integer delete = this.fileTypeService.delete(code);
        return ResponseUtil.success("操作成功").addData("deleteCount", delete);
    }
    @RequestMapping("/batchDelete")
    public Object batchDelete(@RequestBody List<String> codes) {
        Integer integer = this.fileTypeService.batchDelete(codes);

        return ResponseUtil.success("操作成功").addData("deleteCount", integer);
    }
}

